package com.landinfo.web.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.codahale.metrics.annotation.Timed;
import com.landinfo.domain.Authority;
import com.landinfo.repository.AuthorityRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/authority")
public class AuthorityResource {

    private final Logger log = LoggerFactory.getLogger(AuthorityResource.class);

    private static final String ENTITY_NAME = "authority";
    private final AuthorityRepository authorityRepository;


    public AuthorityResource(AuthorityRepository authorityRepository) {
        this.authorityRepository = authorityRepository;
    }


    @PostMapping("/save")
    @Timed
    // @Secured(AuthoritiesConstants.ADMIN)
    public String createAuthority(@RequestBody Authority authority) throws URISyntaxException {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (authority == null) {
            hashMap.put("success", false);
            hashMap.put("message", "传入参数为空");
            JSON.toJSONString(hashMap);
        }
        Authority tempAuthority = this.authorityRepository.save(authority);
        hashMap.put("success", true);
        hashMap.put("rows", tempAuthority);
        return JSON.toJSONString(hashMap);
//        return ResponseEntity.ok(tempAuthority);
    }

    @GetMapping("/find")
    @Timed
    public String getAllAuthority() {
        List<Authority> authorities = this.authorityRepository.findAll();
        return JSON.toJSONString(authorities);
    }


    @GetMapping("/query/{id}")
    @Timed
    public String getUser(@PathVariable String id) {
        Authority authority = this.authorityRepository.findOneByName(id);
//        return JSON.toJSONString(authority);
        if (authority == null) {
            return null;
        } else {
            return JSON.toJSONString(authority,SerializerFeature.DisableCircularReferenceDetect);
        }
    }

//    @DeleteMapping("/del/{id}")
//    @Timed
//    public String deleteUser(@PathVariable String id) {
//        Boolean isOk = true;
//        try {
//            this.authorityRepository.delete(id);
//        } catch (Exception ex) {
//            log.error(ex.getLocalizedMessage());
//            isOk = false;
//        }
//        return isOk.toString();
//    }

    @DeleteMapping("/del/{id}")
    @Timed
    public String delete(@PathVariable String id) throws URISyntaxException {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (id == null || "".equals(id)) {
            hashMap.put("success", false);
            hashMap.put("message", "传入参数为空");
            return JSON.toJSONString(hashMap);
        }
        try {
            hashMap.put("success", true);
            this.authorityRepository.deleteById(id);

        } catch (Exception ex) {
            hashMap.put("success", false);
            hashMap.put("message", ex.getMessage());
        }
        return JSON.toJSONString(hashMap);
    }


}
